<?php
/**
 * Created by PhpStorm.
 * User: hengsu
 * Date: 2016/5/25
 * Time: 18:15
 */

namespace Home\Controller;
use Think\Controller;

class WechatPayController extends Controller{
    public function weixinPay(){
        $userId = isset($_POST['userId'])?trim($_POST['userId']):null;
        $orderId = isset($_POST['orderId'])?trim($_POST['orderId']):null;
        $type = isset($_POST['type'])?trim($_POST['type']):null;
        switch ($type){
            case 1:
                $typestr='datum';
                $datum=M('Datum_order');
                $datumli=$datum->field('price,cat_id')->where('order_id="'.$orderId.'"')->find();
                $total=$datumli['price'];
                $body=$this->getCatBody($datumli['cat_id']);
                break;
            case 2:
                $typestr='testKey';
                $testKey=M('Testkey_order');
                $testKeyli=$testKey->field('price')->where('order_id="'.$orderId.'"')->find();
                $body=$total=$testKeyli['price'];
                break;
            case 3:
                $typestr='book';
                $book=M('Book_order');
                $bookli=$book->field('total_price as price,goods_name')->where('order_id="'.$orderId.'"')->find();
                $total=$bookli['price'];
                $body=$bookli['goods_name'];
                break;
            case 4:
                $typestr='exam';
                $questions=M('Questions_order');
                $quesli=$questions->field('price,cat_id')->where('order_id="'.$orderId.'"')->find();
                $total=$quesli['price'];
                $body=$this->getCatBody($quesli['cat_id']);
                break;
        }
        $user=M('User');
        $userli=$user->field('openId')->where('id='.$userId)->find();
        $requset=PayController::wechatPay($orderId, $body,$total,$typestr,$userli['openId']);
        if(!$requset){
            echo json_encode(array('returnCode'=>999,'returnMsg'=>'调用微信下单失败')); exit;
        }else{
            echo json_encode(array('returnCode'=>0,'returnMsg'=>'成功','returnData'=>$requset)); exit;
        }
    }
    public function wapPay(){
        $orderId = isset($_POST['orderId'])?trim($_POST['orderId']):null;
        $type = isset($_POST['type'])?trim($_POST['type']):null;
        switch ($type){
            case 1:
                $typestr='datum';
                $datum=M('Darum_order');
                $datumli=$datum->field('price,cat_id')->where('order_id="'.$orderId.'"')->find();
				var_dump($datumli);exit;
                $total=$datumli['price'];
                $body=$this->getCatBody($datumli['cat_id']);
                break;
            case 2:
                $typestr='testKey';
                $testKey=M('Testkey_order');
                $testKeyli=$testKey->field('sum(price) as price')->where('order_id="'.$orderId.'"')->select();
                $total=$testKeyli[0]['price'];
                $body=$testKeyli[0]['price'];
                break;
            case 3:
                $typestr='book';
                $book=M('Book_order');
                $bookli=$book->field('total_price as price,goods_name')->where('order_id="'.$orderId.'"')->find();
                $total=$bookli['price'];
                $body=$bookli['goods_name'];
                break;
            case 4:
                $typestr='exam';
                $questions=M('Questions_order');
                $quesli=$questions->field('price,cat_id')->where('order_id="'.$orderId.'"')->find();
				
                $total=$quesli['price'];
                $body=$this->getCatBody($quesli['cat_id']);
                break;
        }
        $requset=PayController::wechatWapPay($orderId, $body,$total,$typestr);
        if(!$requset){
            echo json_encode(array('returnCode'=>999,'returnMsg'=>'调用微信下单失败')); exit;
        }else{
            echo json_encode(array('returnCode'=>0,'returnMsg'=>'成功','returnData'=>$requset)); exit;
        }
    }

    /**
     * @param $catId
     * @return mixed
     */
    public function getCatBody($catId){
        $cate=M('Category');
        $catli=$cate->field('catname')->where('id='.$catId)->find();
        return $catli['catname'];
    }
    public function wechatNotify(){
        $postStr = file_get_contents("php://input");
        if (!empty($postStr)) {
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            if($postObj->return_code == 'SUCCESS'){
                switch ($_GET['type']){
                    case 'datum':
                        $datum=M('Darum_order');
                        $datum->where('order_id="'.$postObj->out_trade_no.'"')->setField(array('status'=>1));
                        break;
                    case 'testKey':
                        $testKey=M('Testkey_order');
                        $testKey->where('order_id="'.$postObj->out_trade_no.'"')->setField(array('status'=>1));
                        break;
                    case 'book':
                        $book=M('Book_order');
                        $book->where('order_id="'.$postObj->out_trade_no.'"')->setField(array('status'=>1));
                        break;
                    case 'exam':
                        $questions=M('Questions_order');
                        $questions->where('order_id="'.$postObj->out_trade_no.'"')->setField(array('status'=>1));
                        break;
                }
                echo '<xml>
  <return_code><![CDATA[SUCCESS]]></return_code>
  <return_msg><![CDATA[OK]]></return_msg>
</xml>';
            }
        }
    }
}